package cn.com.csg.smartenergy.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用于订阅设备Topic
 *
 * @author ysguo
 * @date 2019-07-24
 */
@Slf4j
@Component
public class DeviceDataReportCallBack extends AbstractCallBack {

    @Autowired
    private DeviceDataReportHandler dataReportHandler;

    /**
     * 消息接收回调方法
     * APP主动上报数据topic: @app/terminal/dataReport/@manufacturerId/@manufacturerName/@deviceType/@model/@nodeId
     * 末端设备上线通知接口topic: esdk/notify/event/terminal/status/@manufacturerId/@manufacturerName/@deviceType/@model/@protocolType/@nodeId
     * 末端设备上报紧急事件接口topic: esdk/notify/event/terminal/emergency/@manufacturerId/@manufacturerName/@deviceType/@model /@nodeId
     * 业务APP响应命令topic：@app/terminal/cmdRsp/@manufacturerId/@manufacturerName/@deviceType/@model/@nodeId
     * @param topic   主题
     * @param message 消息
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        // 此处必须要有 try catch 报异常会自动关闭mqtt客户端
        log.info("收到消息：{}", message);
        try {
            this.dataReportHandler.handleDataReportMessage(topic, message, this.getClient());
        } catch (Exception e) {
            log.error(String.format("处理设备上报数据出现异常：Topic: %s", topic), e);
        }
    }
}
